<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" :close-on-press-escape="mode != 'show' ? false : true"
             destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="130">
			<el-form-item :label="$t('goods.bad.badcode')" prop="rejects_code">
				<el-input v-model="form.rejects_code" :placeholder="$t('all.enter')" clearable></el-input>
			</el-form-item>
            <el-form-item :label="$t('goods.bad.badname')" prop="rejects_name">
				<el-input v-model="form.rejects_name" :placeholder="$t('all.enter')" clearable></el-input>
			</el-form-item>
            <el-form-item :label="$t('all.remark')" prop="remark">
				<el-input v-model="form.remark" :placeholder="$t('all.remark')" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >{{$t('all.cancel')}}</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">{{$t('all.save')}}</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
                    add: this.$t('all.add'),
                    edit: this.$t('all.edit'),
                    show: this.$t('all.show')
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					rejects_name: "",
					rejects_code: "",
					remark: "",
				},
				//验证规则
				rules: {
					rejects_name: [
						{required: true, message: this.$t('all.enter') + ' ' + this.$t('goods.bad.badname')}
					]
				},
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						if (this.form.id) {
							res = await this.$API.goods.reason.edit.post(this.form);
						}else{
							res = await this.$API.goods.reason.add.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success(this.$t('all.yes'))
						}else{
							this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.rejects_name = data.rejects_name
				this.form.rejects_code = data.rejects_code
				this.form.remark = data.remark
			}
		}
	}
</script>

<style>
</style>
